#include <sys/time.h>
#include <sys/timeb.h>
#include <time.h>
#include <stdio.h>
#include <errno.h>

#define year 2007
#define month 3
#define day 20
#define hour 12
#define min 0

#define yday 8
static char *wday[]={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
static char *ymon[]={"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
static int mday[]={ 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}; // for 2007

int settime(void)
{
    struct timeval time;
    struct timezone timez;
    unsigned int sec;	

    sec = (((( (year-1970)*365 + mday[month] + day + yday )*24) + hour )*60 + min)*60;
    time.tv_sec= sec;
    time.tv_usec=290944;

    timez.tz_minuteswest=360;
    timez.tz_dsttime=8;

    if( settimeofday(&time, &timez) < 0 ) {
        printf("settimeofday() failed");
        return -1;
    }

    return 0;
}


void main(int argc, char **argv){
	
	time_t timep;
	struct tm *cp;
         
	if(argc<2) {
        		printf("Usage: \n");
        		printf("     tcounter : setup of current time to 2007, Mar, 20, 12:00:00\n");
        		printf("     tcounter 1: display current time\n");		
        	}
	if( argv[1] == NULL ) {
		//printf("init current time \n");
        		settime();	// set current time
	} else {
		time(&timep);
		cp =localtime(&timep); 
		printf ("%d %s %d ", (1900+cp->tm_year),  ymon[cp->tm_mon] , cp->tm_mday);
		printf("%s %d:%d:%d/n", wday[cp->tm_wday],cp->tm_hour, cp->tm_min, cp->tm_sec);
	} 

}

